package classes.controlers.jumpRabbit {
	import classes.controlers.Jump_Rabbits_Controler;
	import classes.managers.Game_Manager;
	import classes.sounds.SoundCollection;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.media.SoundMixer;
	
	
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class JumpRabbit {
		
		private var gm:Game_Manager;
		private var controler:Jump_Rabbits_Controler;
		public var mc:MovieClip;
		
		private var _visible:Boolean;
		
		private var nomal:MovieClip;
		private var ready:MovieClip;
		private var jump:MovieClip;
		private var enter:MovieClip;
		private var fall:MovieClip;
		private var arrow:MovieClip;
		
		private var _readyONOFF:Boolean = false;
		private var _arrowONOFF:Boolean;
		private var _enterEND:Boolean = false;
		
		public function JumpRabbit( _gm:Game_Manager, _controler:Jump_Rabbits_Controler, _mc:MovieClip ) {
			gm = _gm;
			controler = _controler;
			mc = _mc;
			
			nomal = mc.nomal_mc;
			ready = mc.ready_mc;
			jump = mc.jump_mc;
			enter = mc.enter_mc;
			fall = mc.fall_mc;
			arrow = mc.arrow_mc;
			
			init();
			
			addEvent();
		}
		
		private function addEvent():void {
			enter.addEventListener( "enter_END", function(e:Event):void {
				visibleManager( "nomal" );
				enterEND = true;
				//trace("엔터엔드")
			});
			
			jump.addEventListener( "jump_END", function(e:Event):void {
				visibleManager( "nomal" );
				readyONOFF = false;
				SoundCollection.landingSound();
			});
			
			fall.addEventListener( "fall_END", function(e:Event):void {
				gm.gameOver();
				//SoundMixer.stopAll();
			});
			
			ready.addEventListener( "ready_END", function(e:Event):void {
				readyONOFF = true;
			});
		}
		
		public function init():void {
			visible = false;
			fall.gotoAndStop(1);
			visibleManager( "enter" );
			arrowONOFF = false;
			readyONOFF = false;
			enterEND = false;
		}
		
		public function enterPlay():void {
			visibleManager( "enter" );
			enter.play();
			
		}
		
		public function nomalPlay():void {
			readyONOFF = false;
			visibleManager( "nomal" );
			nomal.play();
		}
		
		public function readyPlay():void {
			if ( enterEND ) {
				visibleManager( "ready" );
				ready.gotoAndPlay(1);
				arrowONOFF = false;
			}
		}
		
		public function jumpPlay():void {
			if ( readyONOFF ) {
				visibleManager( "jump" );
				jump.play();
				//jumpSound();
			}
		}
		
		private function jumpSound():void {
			var num:Number = Math.floor(Math.random() * 6);
			switch (num) {
				case 0: SoundCollection.jump1Sound();
				break;
				case 1: SoundCollection.jump2Sound();
				break;
				case 2: SoundCollection.jump3Sound();
				break;
				case 3: SoundCollection.jump4Sound();
				break;
				case 4: SoundCollection.jump5Sound();
				break;
				case 5: SoundCollection.jump6Sound();
				break;
				default:
			}
		}
		
		public function fallPlay():void {
			if ( !readyONOFF ) {
				visibleManager( "fall" );
				fall.play();
				
			}
		}
		
		private function visibleManager( _state:String ):void {
			nomal.visible = false;
			ready.visible = false;
			jump.visible = false;
			enter.visible = false;
			fall.visible  = false;
			//arrow.visible = false;
			
			switch ( _state ) {
				case "nomal": nomal.visible = true;
				break;
				case "ready": ready.visible = true;
				break;
				case "jump": jump.visible = true;
				break;
				case "enter": enter.visible = true;
				break;
				case "fall": fall.visible = true;
				break;
				//case "arrow": arrow.visible = true;
				//break;
				default:
			}
		}
		
		public function get arrowONOFF():Boolean { return _arrowONOFF; }
		public function set arrowONOFF(value:Boolean):void {
			_arrowONOFF = value;
			arrow.visible = value;
		}
		
		public function get readyONOFF():Boolean { return _readyONOFF; }
		public function set readyONOFF(value:Boolean):void {
			_readyONOFF = value;
		}
		
		public function get visible():Boolean { return _visible; }
		public function set visible(value:Boolean):void {
			_visible = value;
			mc.visible = value;
		}
		
		public function get enterEND():Boolean { return _enterEND; }
		public function set enterEND(value:Boolean):void {
			_enterEND = value;
		}
	}
}